package cn.exrick.xboot.modules.portalConfig.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.portalConfig.entity.PortalConfig;
import cn.exrick.xboot.modules.portalConfig.service.IPortalConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "资源配置表管理接口")
@RequestMapping("/xboot/portalConfig")
@Transactional
public class PortalConfigController {

    @Autowired
    private IPortalConfigService iPortalConfigService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<PortalConfig> get(@PathVariable String id){

        PortalConfig portalConfig = iPortalConfigService.getById(id);
        return new ResultUtil<PortalConfig>().setData(portalConfig);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<PortalConfig>> getAll(){
        List<PortalConfig> list = iPortalConfigService.list();
        return new ResultUtil<List<PortalConfig>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<PortalConfig>> getByPage(@ModelAttribute PageVo page){

        IPage<PortalConfig> data = iPortalConfigService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<PortalConfig>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<PortalConfig> saveOrUpdate(@ModelAttribute PortalConfig portalConfig){
        portalConfig.setUpdateTime(new Date());
        if(iPortalConfigService.saveOrUpdate(portalConfig)){
            return new ResultUtil<PortalConfig>().setData(portalConfig);
        }
        return new ResultUtil<PortalConfig>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iPortalConfigService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
    @RequestMapping(value = "/getByKey", method = RequestMethod.GET)
    @ApiOperation(value = "通过key获取")
    public Result<List<PortalConfig>> getByKey( @Param("configKey") String configKey){
        log.info("key:"+configKey);
        List<PortalConfig> list=new ArrayList<>();
        PortalConfig portalConfig = iPortalConfigService.selectPortalConfigByKey(configKey);
        list.add(portalConfig);
        return new ResultUtil<List<PortalConfig>>().setData(list);
    }
}
